package com.sooth.micro.entity.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.dream.system.config.Page;
import com.sooth.common.core.domain.R;
import com.sooth.common.dream.model.PageModel;
import com.sooth.common.log.annotation.Log;
import com.sooth.common.web.core.BaseController;
import com.sooth.micro.entity.service.IMicroEntityService;
import com.sooth.micro.entity.view.MicroEntityBo;
import com.sooth.micro.entity.view.MicroEntityDto;
import com.sooth.micro.entity.view.MicroEntityVo;
import com.sooth.micro.field.service.IMicroFieldService;
import com.sooth.micro.field.view.MicroFieldBo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 实体
 * @Author: moxa
 * @Date: 2023-11-30 19:17:01
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/micro/entity")
public class MicroEntityController extends BaseController {

    private final IMicroEntityService service;

    private final IMicroFieldService fieldService;

    /**
     * 获取实体列表
     */
    @SaCheckPermission("micro:entity:query")
    @GetMapping("/list")
    public R<Page<MicroEntityVo>> list(MicroEntityDto microEntityDto, PageModel pageModel) {
        return R.ok(service.selectPage(microEntityDto, pageModel));
    }

    @SaCheckPermission("micro:entity:query")
    @GetMapping("/listAll")
    public R<List<MicroEntityVo>> listAll(MicroEntityDto microEntityDto) {
        return R.ok(service.selectList(microEntityDto));
    }

    /**
     * 根据实体编号获取详细信息
     *
     * @param id 实体ID
     */
    @SaCheckPermission("micro:entity:query")
    @GetMapping(value = "/query")
    public R<MicroEntityBo> query(@RequestParam Long id) {
        return R.ok(service.selectById(id));
    }

    /**
     * 查询主键
     *
     * @param id
     * @return
     */
    @SaCheckPermission("micro:entity:query")
    @GetMapping(value = "/getPkFields")
    public R<List<MicroFieldBo>> getPkFields(@RequestParam Long id) {
        return R.ok(fieldService.selectPkByEntityId(id));
    }

    /**
     * 新增实体
     */
    @SaCheckPermission("micro:entity:add")
    @Log(title = "实体管理", message = "新增实体")
    @PostMapping("/add")
    public R<Void> save(@Validated @RequestBody MicroEntityBo microEntity) {
        return toAjax(service.insert(microEntity));
    }

    /**
     * 修改实体
     */
    @SaCheckPermission("micro:entity:edit")
    @Log(title = "实体管理", message = "修改实体")
    @PutMapping("/edit")
    public R<Void> edit(@Validated @RequestBody MicroEntityBo microEntity) {
        return toAjax(service.updateById(microEntity));
    }

    /**
     * 删除实体
     *
     * @param id 实体ID
     */
    @SaCheckPermission("micro:entity:remove")
    @Log(title = "实体管理", message = "删除实体")
    @DeleteMapping("/remove")
    public R<Void> remove(@RequestParam Long id) {
        return toAjax(service.deleteById(id));
    }

    @SaCheckPermission("micro:entity:query")
    @GetMapping("/listField")
    public R<List<MicroFieldBo>> listField(@RequestParam Long id) {
        return R.ok(service.listField(id));
    }
}
